home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 20 / Cream of the Crop 20 (Terry Blount) (1996).iso / program / kh_gdi.zip / KH_GDI.TXT < prev    next >
Text File  |  1996-06-03  |  19KB  |  631 lines

  1.  
  2.  
  3. KNOW-HOW GDI Shell v. 1.0
  4. (C) Stepan S.Vartanov, 1995 - 96
  5.  
  6.  
  7.  
  8. 1. Features. 
  9. *    KNOW-HOW GDI Shell v. 1.0 is the WINAPI GDI functions wrapper. 
  10. Its primary purpose is to hide drawing details, such as scaling, 
  11. scrolling and rotations of the image. 
  12. *    ver. 1.0 supports vector graphics only (no bitmaps).
  13. *    KNOW-HOW GDI Shell is a class library, you can instantiate its 
  14. classes or derive your own classes from them.
  15. *    Ver. 1.0 was not tested with Windows 95 - it will be changed in ver. 
  16. 1.2
  17. *    Ver. 1.0 uses Borland BGI fonts for text output (zoom, rotate are 
  18. available). TRUE TYPE fonts support will be added in ver. 1.1 and will 
  19. be available in COMMERCIAL (non-SHAREWARE) version only.
  20.  
  21.  
  22.  
  23. 2. Usage.
  24.  
  25.     As with any class library there are few possible approaches. Some 
  26. suggested approaches are described below. See also KH_DEMO project.
  27. We are going to produce picture of the 'bird'.
  28.  
  29.  
  30.  
  31.  
  32. // Put this code in application main file
  33. // This let you to init drawTool only once
  34.  
  35. #include "drawtool.h"
  36.  
  37. ...
  38.  
  39. YourApplication::Init()
  40.      {
  41.      ...
  42. // Create drawTool in application's destructor
  43.      drawTool = new KH_Paint();
  44.      ...
  45.      }
  46. YourApplication::~YourApplication()
  47.     {
  48.     ....
  49.     delete drawTool;
  50.     ....
  51.     }
  52.  
  53. Use drawTool drawing functions:
  54.  
  55. #include "drawtool.h"
  56.  
  57. MyWindow::draw()
  58.     {
  59.     drawTool->line(100, 100, 200, 300)
  60.     }
  61.  
  62. Another approach (used in example) uses 
  63. inheritance:
  64.  
  65. #include "bgipaint.h"
  66.  
  67. class MyWindow : public KH_Paint
  68.     {
  69.     ...
  70.     drawPig(x, y);    // Your function
  71.     ...
  72.     };
  73.  
  74. ....
  75.  
  76. MyWindow::drawPig(int x, int y)
  77.     {
  78.     line(100, 100, 200, 300);
  79.     ....
  80.     }
  81.  
  82.  
  83.  
  84. 3. Classes and Functions. 
  85.  
  86.  
  87. File BGI_FONT.H
  88.     Class encapsulates BGI fonts (*.chr) description. BGI fonts are not 
  89. documented. I use existing format.
  90.  
  91. struct _export BGI_Font : public AbstractGraphics
  92.     {
  93.       ...
  94. // Deformation of an image
  95.     uchar multx, multy, divx, divy;       
  96. // Create object and load() (BGI) font. 
  97.     BGI_Font(char* fileName = 0);  
  98.     ~BGI_Font();
  99. // Remove old and load new font. Fomt should 
  100. // exist, or NULL is loaded
  101.     void load(char* fileName);     
  102. // Character output
  103.     int draw_char(int x,int y,char c, int dir=0);
  104. // Line of characters
  105.     int outtextxy(int x, int y, char* str, int 
  106.           dir = 0);
  107. // Overload when API accessible
  108.     void outtext(char* str) {} 
  109. // Width of text, pixels (not rotated)
  110.     int gettextwidth(char*  str);
  111. // Heigth of text, pixels (not rotated)
  112.     int getheight() { return CapitalHeight; }
  113. // Set character deformation
  114.     void setusercharsize(int mx, int dx, int my, 
  115.           int dy)
  116.         {multx=mx; multy=my; divx=dx; divy=dy; }
  117. // Set text justification
  118.     void settextjustify(int j,int k) 
  119.          {h_just=j;v_just = k; }
  120.     };
  121.  
  122.  
  123.  
  124. FILE DRAWTOOL.H
  125.  
  126.     Defines global drawing object - in the case if you do not want to use 
  127. inheritance. This is not part of library - just an example.:
  128.  
  129.     #ifndef __KH_PAINT_H_
  130.     #define __KH_PAINT_H_
  131.  
  132.     #include "bgipaint.h"
  133.     extern KH_Paint* drawTool;
  134.  
  135.  
  136.  
  137. FILE BGIPAINT.H
  138.     User will probably work only with this file. Real API functions of 
  139. Windows are available to this class(es).
  140.  
  141. // Convenient names - you can use any set - Windows, if your are more 
  142. used to it, or Borland BGI-like names:
  143.     #define SOLID_FILL              1
  144.     #define SOLID_LINE              PS_SOLID
  145.     #define DASHED_LINE             PS_DASH
  146.     #define DOTTED_LINE             PS_DOT
  147.     #define CENTER_LINE             PS_DASHDOT
  148.  
  149. // Do not use HS_BDIAGONAL, HS_FDIAGONAL, 
  150. // VS_VERTICAL, HS_HORIZONTAL HS_CROSS and 
  151. // HS_DIAGCROSS !!!
  152.  
  153.     #define LINE_FILL               0
  154.     #define HATCH_FILL              2
  155.     #define SLASH_FILL              3
  156.     #define XHATCH_FILL             4
  157.     #define BKSLASH_FILL            5
  158.     #define LTSLASH_FILL            6
  159.  
  160.     #define LEFT_TEXT     0
  161.     #define CENTER_TEXT   1
  162.     #define RIGHT_TEXT    2
  163.     #define BOTTOM_TEXT   0
  164.     #define TOP_TEXT      2
  165.  
  166.     #define HS_SOLID                SOLID_FILL
  167.  
  168.     enum COLORS { BLACK, BLUE, GREEN, CYAN, RED, 
  169. MAGENTA, BROWN, LIGHTGRAY, DARKGRAY, LIGHTBLUE, 
  170. LIGHTGREEN, LIGHTCYAN, LIGHTRED, LIGHTMAGENTA, 
  171. YELLOW, WHITE };
  172.  
  173. // Use this structure to reference collors in 16 
  174. colors scale
  175.     struct color_to_16
  176.      {
  177.      int color;
  178.      COLORREF colorref;
  179.      };
  180.     // Descriptions of colors
  181.     static color_to_16 tricolors[16] =
  182.      {   { BLACK, 0 },
  183.           { BLUE, RGB(0, 0, 128) },
  184.           { GREEN, RGB(0, 128, 0) },
  185.           {  CYAN, RGB(0, 128, 128) },
  186.           {  RED, RGB(128, 0, 0) },
  187.           { MAGENTA, RGB(74, 23, 74) },
  188.           { BROWN, RGB(128, 64, 64) },
  189.           { LIGHTGRAY, RGB(128, 128, 128) },
  190.           { DARKGRAY, RGB(64, 64, 64) },
  191.           { LIGHTBLUE, RGB(0, 0, 255) },
  192.           { LIGHTGREEN, RGB(0, 255, 0) },
  193.           { LIGHTCYAN, RGB(0, 255, 255) },
  194.           { LIGHTRED, RGB(255, 0, 0) },
  195.           { LIGHTMAGENTA, RGB(255, 0, 255) },
  196.           { YELLOW, RGB(255,255,30) },
  197.           { WHITE, RGB(255, 255, 255) }
  198.      };
  199.  
  200. // There are some reasons why this class is not 
  201. part of 
  202. // KH_Paint. However under normal circumstances 
  203. user will never use it 
  204. // directly
  205.     struct _export To_Paint
  206.      {
  207.      int PenSize;
  208.      int PenStyle;
  209.      COLORREF color;
  210.  
  211.      int pattern_num;
  212.      int fill_color;
  213.      COLORREF BrushColor;
  214.  
  215.      HDC DC;
  216. /////////////////////////////////
  217.      To_Paint() { pattern_num = SOLID_FILL; 
  218.            PenSize = 1; PenStyle = PS_SOLID; 
  219.            color = RGB(0,0,0); 
  220.          BrushColor = RGB(0,0,0); }
  221. /////////////////////////////////
  222.      int getx() 
  223.         { POINT p; ::GetCurrentPositionEx(DC, 
  224.              &p); return p.x; }
  225.      int gety() 
  226.         { POINT p; ::GetCurrentPositionEx(DC, 
  227.              &p); return p.y; }
  228.      COLORREF getcolorref() { return color; }
  229.      int getcolor(COLORREF col);
  230.      int getcolor() { return getcolor(color); }
  231.  
  232.      void setcolor(int r, int g, int b)    
  233.         { color = RGB(r,g,b); }
  234.      void setcolor(int c)
  235.     { color = tricolors[c].colorref; }
  236.  
  237.      void putpixel(int x, int y) 
  238.         { ::SetPixel(DC, x, y, getcolorref()); }
  239.      void setlinestyle(int width, int style)
  240.         { PenSize = width; PenStyle = style; }
  241.     void setlinestyle(int style, unsigned int, 
  242.             int width)
  243.         { PenSize = width; PenStyle = style; }
  244.  
  245.     void setfillstyle(int style, int col)
  246.         {
  247.         pattern_num = style;
  248.         fill_color = col;
  249.         BrushColor = tricolors[col].colorref;
  250.         }
  251.     void setfillstyle(int style, COLORREF col)
  252.         {
  253.         pattern_num = style;
  254.         BrushColor = col;
  255.         }
  256.  
  257.     void moveto(int x, int y) { ::MoveTo(DC, x, 
  258.             y); }
  259.         void lineto(int x, int y);
  260.         void fillpoly(int numpoints, int* 
  261.             polypoints);
  262.         void drawpoly(int numpoints, int far* 
  263.             points);
  264.         };
  265.  
  266.  
  267.  
  268. File BGIPAINT.H
  269.  
  270. KH_Paint class could be considered as the main class in the library. 
  271.  
  272.     struct _export KH_Paint : public To_Paint, 
  273.                      public Paint
  274.     {
  275.     KH_Paint() : To_Paint(), Paint() {}
  276.  
  277.       int getx();
  278.       int gety();
  279.     loc get_CP();
  280.     void putpixel(int x, int y);
  281.     void line(int xstart, int ystart, int xend, 
  282.         int yend)
  283.         { moveto(xstart, ystart); lineto(xend, 
  284.         yend); }
  285.  
  286.     void lineto(int x, int y);
  287.     void moveto(int x, int y);
  288.     void circle(int x, int y, int radius)
  289.         { ellipse(x, y, 0, 360, radius, radius);}
  290.     void ellipse(int x, int y, int stangle, int 
  291.          endangle, int xr, int yr);
  292.     void rectangle(int left, int top, int right, 
  293.          int bottom);
  294.     void drawpoly(int numpoints, int far* 
  295.          points);
  296.     void fillpoly(int numpoints, int far* points)
  297.         { int f = fill; fill = ON; 
  298.           drawpoly(numpoints,points); fill = f; }
  299.     void bar3d(int l, int t, int r, int b, int d, 
  300.           int top);
  301.     virtual void outtext(char* str, int dir = 0);
  302.     void set_fill(int state) { fill = state; }
  303.     };
  304.  
  305.  
  306.  
  307. FILE PAINT.H
  308.  
  309.     Encapsulates axes transformation routines. Rotation could be 
  310. simply processed if it is single operation. To use nested rotations we use 
  311. stack of rotations info structures. 
  312.  
  313.     Example of complex rotation:
  314.          void f1(int x, int y, int alpha) 
  315.         { ... perform rotation ... }
  316.          void f2(int x, int y, int alpha) 
  317.         { rotate(10,10,90); f1(x, y, alpha); }
  318.     Fu